package com.cvte.boe.media.views;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

/**
 * Created by wuguitong on 2018/5/2.
 */
public class AutoHideLayout extends RelativeLayout{
    private static final int AUTO_HIDE_TIME = 2000;
    private static final int MSG_SHOW = 0;
    private static final int MSG_HIDE = 1;
    private boolean bShow = false;
    private Handler handler;
    public AutoHideLayout(Context context) {
        this(context,null);
    }

    public AutoHideLayout(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public AutoHideLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what){
                    case MSG_HIDE:
                        synchronized (AutoHideLayout.class) {
                            setVisibility(GONE);
                            bShow = false;
                        }
                        break;
                    case MSG_SHOW:
                        synchronized (AutoHideLayout.class) {
                            setVisibility(VISIBLE);
                            bShow = true;
                        }
                        break;
                    default:
                        break;
                }
            }
        };
        synchronized (AutoHideLayout.class) {
            setVisibility(GONE);
            bShow = false;
        }
    }
    public void show() {
        synchronized (AutoHideLayout.class) {
            handler.removeMessages(MSG_HIDE);
            setVisibility(VISIBLE);
            bShow = true;
            handler.sendEmptyMessageDelayed(MSG_HIDE, AUTO_HIDE_TIME);
        }
    }
    public void hide(){
        synchronized (AutoHideLayout.class) {
            setVisibility(GONE);
            handler.removeMessages(MSG_HIDE);
            bShow = false;
        }
    }

    public boolean isShow(){
        return bShow;
    }


}
